package com.saifan.wyy_ov.ui.onlishop;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CartBean;
import com.saifan.wyy_ov.data.bean.CartBean.GoodsList;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.MaterialDialog;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.ToastUtil;
public class AdapterCartItem extends BaseAdapter {
	Context context;
	CartBean cartBean;
	boolean isChecked = true;
	Handler handler;
	static int DELETESHOPPINGCART=22;

	public boolean isChecked() {
		return isChecked;
	}
	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
	}
	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
		for (GoodsList goodsList : cartBean.getGoodsList()) {
			goodsList.setIspay( isChecked);
		}
	}
	public AdapterCartItem(Context context, CartBean cartBean, Handler handler) {
		this.context = context;
		this.cartBean = cartBean;
		this.handler = handler;
	}
	@Override
	public int getCount() {
		if (cartBean.getGoodsList()!=null&&!cartBean.getGoodsList().isEmpty()  )
			return cartBean.getGoodsList().size();
		else
			return 0;
	}
	@Override
	public Object getItem(int position) {
		if (cartBean.getGoodsList()!=null&&!cartBean.getGoodsList().isEmpty()  )
			return cartBean.getGoodsList().get(position);
		else
			return null;
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder(context, cartBean.getGoodsList().get(position),handler);
			convertView = viewHolder.convertView;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.setGoodsList(cartBean.getStoreID(),cartBean.getGoodsList().get(position));
		return convertView;
	}
	static class ViewHolder implements OnCheckedChangeListener ,OnClickListener,OnLongClickListener {
		ImageView iv_goods_image;
		TextView tv_goods_name;
		TextView tv_count;
		TextView tv_total;
		CheckBox cb_selelct;
		Context context;
		GoodsList goodsList;
		public static MaterialDialog dialog;
		int storeID;
		public GoodsList getGoodsList() {
			return goodsList;
		}
		public void setGoodsList(int storeID,GoodsList goodsList) {
			this.goodsList = goodsList;
			this.storeID=storeID;
			ImgUtil.DisPlayImg(goodsList.getCoverPhoto(),  iv_goods_image, context);
			 tv_goods_name.setText(goodsList.getGoodsName());
			 tv_count.setText( goodsList.getCount() + "");
			double total = goodsList.getCount() * goodsList.getGoodsPrice();
			tv_total.setText( PriceUtils.getPrice(total)   +"元");
			cb_selelct.setChecked(goodsList.isIspay());
			cb_selelct.setOnCheckedChangeListener(this);
		}
		View convertView;
		Handler handler;
		public ViewHolder(Context context, GoodsList goodsList,Handler handler) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.goodsList = goodsList;
			this.handler=handler;
			convertView = LayoutInflater.from(context).inflate(R.layout.cart_item_view, null);
			convertView.setOnClickListener(this);
			convertView.setOnLongClickListener(this);
			iv_goods_image = (ImageView) convertView.findViewById(R.id.iv_goods_image);
			 
			tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
			tv_count = (TextView) convertView.findViewById(R.id.tv_count);
			tv_total = (TextView) convertView.findViewById(R.id.tv_total);
			cb_selelct = (CheckBox) convertView.findViewById(R.id.cb_select);
		}
		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			// TODO Auto-generated method stub
			goodsList.setIspay(arg1);
			Message message = new Message();
			message.what = 12;
			message.obj=arg1;
			//这一项商品取消
			goodsList.setIspay(arg1);
			handler.sendMessage(message);
		}
		@Override
		public void onClick(View paramView) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(context, GoodsDetailActivity.class);
			 intent.putExtra("storeId",  String.valueOf(storeID));
			 intent.putExtra("goodsId", String.valueOf(goodsList.getGoodsID()));
			 intent.putExtra("goodsName", goodsList.getGoodsName());
			 intent.putExtra("price",  String.valueOf(goodsList.getGoodsPrice()));
			 context.startActivity(intent);
			 
			 

		}
		@Override
		public boolean onLongClick(View v) {
				dialog= new MaterialDialog(context);
				dialog.setTitle("提示").setMessage("从购物车删除这个商品").setPositiveButton("是", new OnClickListener() {
					@Override
					public void onClick(View v) {
						 dialog.dismiss();
						 CommonDataBean commonDataBean= new CommonDataBean();
						 commonDataBean.setID(goodsList.getID());
						 commonDataBean.setShoppingCartID(goodsList.getShoppingCartID());
						 GetData getData= new GetData();
						 getData.getDataJson(context, Api.GroupBuy.DELETESHOPPINGCART, commonDataBean, "正在删除,请稍候", new ResponseData() {
							
						

							@Override
							public void onSuccess(String json) {
								// TODO Auto-generated method stub
								ToastUtil.showToast(context, "删除 成功");
								Message message = new Message();
								message.what=DELETESHOPPINGCART;
								message.obj=goodsList;
								handler.sendMessage(message);
								
							}
							
							@Override
							public void onFailed(String str) {
								// TODO Auto-generated method stub
								ToastUtil.showToast(context, "删除出错,请重试");
							}
						});
						 
						  
					}
				}).setNegativeButton("否", new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				}).setCanceledOnTouchOutside(true)
				.setOnDismissListener(new DialogInterface.OnDismissListener() {
					@Override
					public void onDismiss(DialogInterface dialog) {
					}
				}).show();
			 
			
			return true;
		}
		
	}
}
